load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("@rules_license//rules:license.bzl", "license")

package(default_applicable_licenses = [":license"])

license(
    name = "license",
    license_kinds = ["@rules_license//licenses/spdx:BSD-3-Clause"],
    license_text = "LICENSE",
)

cc_binary(
    name = "iperf3",
    srcs = ["src/main.c"],
    visibility = ["//visibility:public"],
    deps = [":libiperf"],
)

cc_library(
    name = "libiperf",
    srcs = glob(
        ["src/**/*.c"],
        exclude = [
            "src/main.c",
            "src/t_api.c",
            "src/t_auth.c",
            "src/t_timer.c",
            "src/t_units.c",
            "src/t_uuid.c",
        ],
    ) + [
        # This is not exposed in any public headers.
        ":version_hdr",
    ],
    hdrs = glob(["src/**/*.h"]) + [":iperf_config_hdr"],
    includes = ["src"],
    visibility = ["//visibility:public"],
    deps = ["@openssl//:ssl"],
)

expand_template(
    name = "iperf_config_hdr",
    out = "iperf_config.h",
    substitutions = {
        "#undef HAVE_CLOCK_GETTIME\n": "#define HAVE_CLOCK_GETTIME 1\n",
        "#undef HAVE_CLOCK_NANOSLEEP\n": "#define HAVE_CLOCK_NANOSLEEP 1\n",
        "#undef HAVE_CPUSET_SETAFFINITY\n": "/* #undef HAVE_CPUSET_SETAFFINITY */\n",
        "#undef HAVE_CPU_AFFINITY\n": "#define HAVE_CPU_AFFINITY 1\n",
        "#undef HAVE_DAEMON\n": "#define HAVE_DAEMON 1\n",
        "#undef HAVE_DLFCN_H\n": "#define HAVE_DLFCN_H 1\n",
        "#undef HAVE_DONT_FRAGMENT\n": "#define HAVE_DONT_FRAGMENT 1\n",
        "#undef HAVE_ENDIAN_H\n": "#define HAVE_ENDIAN_H 1\n",
        "#undef HAVE_FLOWLABEL\n": "#define HAVE_FLOWLABEL 1\n",
        "#undef HAVE_GETLINE\n": "#define HAVE_GETLINE 1\n",
        "#undef HAVE_INTTYPES_H\n": "#define HAVE_INTTYPES_H 1\n",
        "#undef HAVE_IP_DONTFRAG\n": "/* #undef HAVE_IP_DONTFRAG */\n",
        "#undef HAVE_IP_DONTFRAGMENT\n": "/* #undef HAVE_IP_DONTFRAGMENT */\n",
        "#undef HAVE_IP_MTU_DISCOVER\n": "#define HAVE_IP_MTU_DISCOVER 1\n",
        "#undef HAVE_LINUX_TCP_H\n": "#define HAVE_LINUX_TCP_H 1\n",
        "#undef HAVE_NANOSLEEP\n": "#define HAVE_NANOSLEEP 1\n",
        "#undef HAVE_NETINET_SCTP_H\n": "/* #undef HAVE_NETINET_SCTP_H */\n",
        "#undef HAVE_POLL_H\n": "#define HAVE_POLL_H 1\n",
        "#undef HAVE_PTHREAD\n": "#define HAVE_PTHREAD 1\n",
        "#undef HAVE_PTHREAD_PRIO_INHERIT\n": "#define HAVE_PTHREAD_PRIO_INHERIT 1\n",
        "#undef HAVE_SCHED_SETAFFINITY\n": "#define HAVE_SCHED_SETAFFINITY 1\n",
        "#undef HAVE_SCTP_H\n": "/* #undef HAVE_SCTP_H */\n",
        "#undef HAVE_SENDFILE\n": "#define HAVE_SENDFILE 1\n",
        "#undef HAVE_SETPROCESSAFFINITYMASK\n": "/* #undef HAVE_SETPROCESSAFFINITYMASK */\n",
        "#undef HAVE_SO_BINDTODEVICE\n": "#define HAVE_SO_BINDTODEVICE 1\n",
        "#undef HAVE_SO_MAX_PACING_RATE\n": "#define HAVE_SO_MAX_PACING_RATE 1\n",
        "#undef HAVE_SSL\n": "#define HAVE_SSL 1\n",
        "#undef HAVE_STDATOMIC_H\n": "#define HAVE_STDATOMIC_H 1\n",
        "#undef HAVE_STDINT_H\n": "#define HAVE_STDINT_H 1\n",
        "#undef HAVE_STDIO_H\n": "#define HAVE_STDIO_H 1\n",
        "#undef HAVE_STDLIB_H\n": "#define HAVE_STDLIB_H 1\n",
        "#undef HAVE_STRINGS_H\n": "#define HAVE_STRINGS_H 1\n",
        "#undef HAVE_STRING_H\n": "#define HAVE_STRING_H 1\n",
        "#undef HAVE_STRUCT_SCTP_ASSOC_VALUE\n": "/* #undef HAVE_STRUCT_SCTP_ASSOC_VALUE */\n",
        "#undef HAVE_SYS_ENDIAN_H\n": "/* #undef HAVE_SYS_ENDIAN_H */\n",
        "#undef HAVE_SYS_SOCKET_H\n": "#define HAVE_SYS_SOCKET_H 1\n",
        "#undef HAVE_SYS_STAT_H\n": "#define HAVE_SYS_STAT_H 1\n",
        "#undef HAVE_SYS_TYPES_H\n": "#define HAVE_SYS_TYPES_H 1\n",
        "#undef HAVE_TCP_CONGESTION\n": "#define HAVE_TCP_CONGESTION 1\n",
        "#undef HAVE_TCP_INFO_SND_WND\n": "#define HAVE_TCP_INFO_SND_WND 1\n",
        "#undef HAVE_TCP_USER_TIMEOUT\n": "#define HAVE_TCP_USER_TIMEOUT 1\n",
        "#undef HAVE_UNISTD_H\n": "#define HAVE_UNISTD_H 1\n",
        "#undef LT_OBJDIR\n": '#define LT_OBJDIR ".libs/"\n',
        "#undef PACKAGE\n": '#define PACKAGE "iperf"\n',
        "#undef PACKAGE_BUGREPORT\n": '#define PACKAGE_BUGREPORT "https://github.com/esnet/iperf"\n',
        "#undef PACKAGE_NAME\n": '#define PACKAGE_NAME "iperf"\n',
        "#undef PACKAGE_STRING\n": '#define PACKAGE_STRING "iperf 3.18"\n',
        "#undef PACKAGE_TARNAME\n": '#define PACKAGE_TARNAME "iperf"\n',
        "#undef PACKAGE_URL\n": '#define PACKAGE_URL "https://software.es.net/iperf/"\n',
        "#undef PACKAGE_VERSION\n": '#define PACKAGE_VERSION "3.18"\n',
        "#undef PTHREAD_CREATE_JOINABLE\n": "/* #undef PTHREAD_CREATE_JOINABLE */\n",
        "#undef STDC_HEADERS\n": "#define STDC_HEADERS 1\n",
        "#undef VERSION\n": '#define VERSION "3.18"\n',
        "#undef const\n": "/* #undef const */\n",
    },
    template = "src/iperf_config.h.in",
)

expand_template(
    name = "version_hdr",
    out = "version.h",
    substitutions = {"@PACKAGE_VERSION@": "3.18"},
    template = "src/version.h.in",
)

cc_test(
    name = "t_api",
    size = "small",
    srcs = ["src/t_api.c"],
    copts = ["-UNDEBUG"],
    deps = [":libiperf"],
)

cc_test(
    name = "t_timer",
    size = "small",
    srcs = ["src/t_timer.c"],
    copts = ["-UNDEBUG"],
    deps = [":libiperf"],
)

cc_test(
    name = "t_units",
    size = "small",
    srcs = ["src/t_units.c"],
    copts = ["-UNDEBUG"],
    deps = [":libiperf"],
)

cc_test(
    name = "t_uuid",
    size = "small",
    srcs = ["src/t_uuid.c"],
    copts = ["-UNDEBUG"],
    deps = [":libiperf"],
)

# NOTE: The t_auth test is disabled for now to avoid dealing with runfiles in a C binary.
